package com.hotkid.ceo.jetpack.myjetpackmvvm.base

import android.os.Bundle
import android.view.View
import androidx.lifecycle.ViewModelProvider
import androidx.viewbinding.ViewBinding
import com.hotkid.ceo.jetpack.myjetpackmvvm.common.MyConfig
import com.yechaoa.yutilskt.SpUtil

/**
 * ClassName:BaseVmFragment
 * Package:com.hotkid.ceo.jetpack.myjetpackmvvm.base
 * Description:
 * @date:2021/3/23 8:22 PM
 * @author:774169396@qq.com
 */
abstract class BaseVmFragment<VB:ViewBinding,VM:BaseViewModel>:BaseFragment<VB>() {
    protected lateinit var mViewModel: VM
    private var lazyLoaded=false
    //分页的参数
    protected open val mToalCount=20//每次加载的数量
    protected open var mCurrentSize=0//
    protected open var mCurrentPage=0//当前加载的页数

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initViewModel()
        observe()
        initView()
        initData()
        setListeneres()
    }

    override fun onResume() {
        super.onResume()
        if (!lazyLoaded){
            lazyLoadData()
            lazyLoaded=true
        }
    }

    private fun lazyLoadData() {

    }

    private fun setListeneres() {

    }

    private fun initData() {

    }

    private fun initView() {

    }

    /**
     * 订阅，逻辑千万需要super
     */
    open fun observe() {
        mViewModel.needLogin.observe(viewLifecycleOwner, {
            if (it) {
                SpUtil.setBoolean(MyConfig.IS_LOGIN, false)
                // TODO: 2021/3/23 调整登录页
            }
        })
    }

    /**
     * 初始化ViewModel
     */
    private fun initViewModel() {
       mViewModel=ViewModelProvider(this).get(viewModelClass())
    }

    /**
     * 获取viewMODED的程class
     */
    abstract fun viewModelClass(): Class<VM>

    override fun onDestroyView() {
        super.onDestroyView()
        //
        mCurrentPage=0
        mCurrentSize=0
    }
}